package xj.graph2d.template.uml;

import xj.graph2d.DrawShape;
import xj.graph2d.GraphNodeBase;
import xj.graph2d.Label;
import xj.graph2d.shapes.uml.OrthogonalStateShape;
import xj.graph2d.shapes.uml.StateShape;
import xj.graph2d.template.UMLTemplate;
import xj.model.uml.OrthogonalState;
import xj.model.uml.State;
import xj.model.uml.SubmachineState;

public class UMLStateModelListener extends UMLAbstractModelListener {

  public UMLStateModelListener() {
  }

  public UMLStateModelListener(Object model, DrawShape shape,
      UMLTemplate template) {
    super(model, shape, template);
  }

  @Override
  public void initModel() {
    if (shape instanceof OrthogonalStateShape && 
	model instanceof OrthogonalState) {
      OrthogonalState os = (OrthogonalState) model;
      OrthogonalStateShape cs = (OrthogonalStateShape) shape;
      int n = cs.getNumberOfRegions();
      os.makeRegions(n);
    }
  }

  @Override
  public void updateModel() {
    if (model instanceof State) {
      if (debug) { 
	System.out.println("UMLStateModelLisener.updateModel(): shape=" + shape);
      }
      State smodel = (State) model;
      if (shape instanceof StateShape) {
	StateShape snode = (StateShape) shape;
	String name = snode.getStateName();
	if (name != null) {
	  if (model instanceof SubmachineState) {
	    SubmachineState sm = (SubmachineState) model;
	    String submachineName = null;
	    int i = name.indexOf(':');
	    if (i >= 0) {
	      submachineName = name.substring(i + 1).trim();
	      name = name.substring(0, i).trim();
	    }
	    sm.setName(name);
	    sm.setSubmachineName(submachineName);
	  } else {
	    smodel.setName(name);
	  }
	}

	String st = snode.getStateStereotypes();
	smodel.setStereotypes(st);
	snode.getActions(smodel);
      } else if (shape instanceof GraphNodeBase) {
	GraphNodeBase snode = (GraphNodeBase) shape;
	String name = snode.getLabel();
	if (name != null) {
	  smodel.setName(name);
	}
      }
    }
  }

  @Override
  public void updateShape() {
    if (model instanceof State) {
      if (debug) { 
	System.out.println("UMLStateModelLisener.updateShape(): shape=" + shape);
      }
      State smodel = (State) model;
      if (shape instanceof StateShape) {
	StateShape snode = (StateShape) shape;
	String name = smodel.getName();

	if (model instanceof SubmachineState) {
	  SubmachineState sm = (SubmachineState) model;
	  String submachineName = sm.getSubmachineName();
	  if (submachineName != null && submachineName.length() > 0) {
	    name += (" : " + submachineName);
	  }
	  snode.setStateName(name);
	} else {
	  snode.setStateName(name);
	}

	String st = smodel.getStereotypeString();
	snode.setStateStereotypes(st);
	snode.setActions(smodel);
      } else if (shape instanceof GraphNodeBase) {
	GraphNodeBase snode = (GraphNodeBase) shape;
	String name = smodel.getName();
	if (name != null) {
	  snode.setLabel(name);
	  Label label = snode.getLabelShape();
	  label.setDetachable(false);
	  label.setDisplayName("Name");
	}
      }
    }
  }

}
